package cn.iocoder.yudao.module.eval.controller.admin.profession;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.module.eval.controller.admin.profession.vo.*;
import cn.iocoder.yudao.module.eval.dal.dataobject.profession.EvalProfessionDO;
import cn.iocoder.yudao.module.eval.service.profession.EvalProfessionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import cn.iocoder.yudao.framework.common.pojo.PageParam;

@Tag(name = "管理后台 - 职业工种")
@RestController
@RequestMapping("/eval/profession")
@Validated
public class EvalProfessionController {

    @Resource
    private EvalProfessionService professionService;

    @PostMapping("/create")
    @Operation(summary = "创建职业工种")
    @PreAuthorize("@ss.hasPermission('eval:profession:create')")
    public CommonResult<Long> createProfession(@Valid @RequestBody EvalProfessionSaveReqVO createReqVO) {
        return success(professionService.createProfession(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新职业工种")
    @PreAuthorize("@ss.hasPermission('eval:profession:update')")
    public CommonResult<Boolean> updateProfession(@Valid @RequestBody EvalProfessionSaveReqVO updateReqVO) {
        professionService.updateProfession(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除职业工种")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('eval:profession:delete')")
    public CommonResult<Boolean> deleteProfession(@RequestParam("id") Long id) {
        professionService.deleteProfession(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得职业工种")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('eval:profession:query')")
    public CommonResult<EvalProfessionRespVO> getProfession(@RequestParam("id") Long id) {
        EvalProfessionDO profession = professionService.getProfession(id);
        return success(BeanUtils.toBean(profession, EvalProfessionRespVO.class));
    }

    @GetMapping("/list")
    @Operation(summary = "获得职业工种列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('eval:profession:query')")
    public CommonResult<List<EvalProfessionRespVO>> getProfessionList(@RequestParam("ids") Collection<Long> ids) {
        List<EvalProfessionDO> list = professionService.getProfessionList(ids);
        return success(BeanUtils.toBean(list, EvalProfessionRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得职业工种分页")
    @PreAuthorize("@ss.hasPermission('eval:profession:query')")
    public CommonResult<PageResult<EvalProfessionRespVO>> getProfessionPage(@Valid EvalProfessionPageReqVO pageReqVO) {
        PageResult<EvalProfessionDO> pageResult = professionService.getProfessionPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EvalProfessionRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出职业工种 Excel")
    @PreAuthorize("@ss.hasPermission('eval:profession:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProfessionExcel(@Valid EvalProfessionPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EvalProfessionDO> list = professionService.getProfessionPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "职业工种.xls", "数据", EvalProfessionRespVO.class,
                BeanUtils.toBean(list, EvalProfessionRespVO.class));
    }

    @GetMapping("/list-by-approval-status")
    @Operation(summary = "根据审核状态获得职业工种列表")
    @Parameter(name = "approvalStatus", description = "审核状态", required = true, example = "1")
    @PreAuthorize("@ss.hasPermission('eval:profession:query')")
    public CommonResult<List<EvalProfessionRespVO>> getProfessionListByApprovalStatus(@RequestParam("approvalStatus") Integer approvalStatus) {
        List<EvalProfessionDO> list = professionService.getProfessionListByApprovalStatus(approvalStatus);
        return success(BeanUtils.toBean(list, EvalProfessionRespVO.class));
    }

    @GetMapping("/list-by-category")
    @Operation(summary = "根据职业大类获得职业工种列表")
    @Parameter(name = "category", description = "职业大类", required = true, example = "制造业")
    @PreAuthorize("@ss.hasPermission('eval:profession:query')")
    public CommonResult<List<EvalProfessionRespVO>> getProfessionListByCategory(@RequestParam("category") String category) {
        List<EvalProfessionDO> list = professionService.getProfessionListByCategory(category);
        return success(BeanUtils.toBean(list, EvalProfessionRespVO.class));
    }

    @PutMapping("/audit")
    @Operation(summary = "审核职业工种")
    @PreAuthorize("@ss.hasPermission('eval:profession:audit')")
    public CommonResult<Boolean> auditProfession(@Valid @RequestBody EvalProfessionAuditReqVO auditReqVO) {
        professionService.auditProfession(auditReqVO.getId(), auditReqVO.getAuditStatus(), auditReqVO.getAuditComments());
        return success(true);
    }

    @GetMapping("/match")
    @Operation(summary = "岗位职业匹配查询")
    @Parameter(name = "keyword", description = "关键词", required = true, example = "电工")
    @Parameter(name = "category", description = "职业大类", required = false, example = "制造业")
    @PreAuthorize("@ss.hasPermission('eval:profession:query')")
    public CommonResult<List<EvalProfessionRespVO>> matchProfessions(@RequestParam("keyword") String keyword,
                                                                     @RequestParam(value = "category", required = false) String category) {
        List<EvalProfessionRespVO> list = professionService.matchProfessions(keyword, category);
        return success(list);
    }

    @GetMapping("/search")
    @Operation(summary = "职业工种搜索")
    @Parameter(name = "keyword", description = "搜索关键词", required = true, example = "电工")
    @PreAuthorize("@ss.hasPermission('eval:profession:query')")
    public CommonResult<List<EvalProfessionRespVO>> searchProfessions(@RequestParam("keyword") String keyword) {
        List<EvalProfessionRespVO> list = professionService.searchProfessions(keyword);
        return success(list);
    }

    @GetMapping("/statistics")
    @Operation(summary = "职业工种统计")
    @PreAuthorize("@ss.hasPermission('eval:profession:query')")
    public CommonResult<Object> getProfessionStatistics() {
        return success(professionService.getProfessionStatistics());
    }

    @PostMapping("/batch-audit")
    @Operation(summary = "批量审核职业工种")
    @PreAuthorize("@ss.hasPermission('eval:profession:audit')")
    @ApiAccessLog(operateType = UPDATE)
    public CommonResult<Boolean> batchAuditProfessions(@Valid @RequestBody List<EvalProfessionAuditReqVO> auditReqVOs) {
        professionService.batchAuditProfessions(auditReqVOs);
        return success(true);
    }

    @PutMapping("/update-status/{id}")
    @Operation(summary = "更新职业状态")
    @Parameter(name = "id", description = "职业ID", required = true)
    @Parameter(name = "approvalStatus", description = "审核状态", required = true)
    @Parameter(name = "approvalComments", description = "审核意见", required = false)
    @PreAuthorize("@ss.hasPermission('eval:profession:audit')")
    public CommonResult<Boolean> updateProfessionStatus(@PathVariable("id") Long id,
                                                        @RequestParam("approvalStatus") Integer approvalStatus,
                                                        @RequestParam(value = "approvalComments", required = false) String approvalComments) {
        professionService.updateProfessionStatus(id, approvalStatus, approvalComments);
        return success(true);
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获得职业工种精简信息列表", description = "只包含已审批的职业工种，主要用于前端的下拉选项")
    @PreAuthorize("@ss.hasPermission('eval:profession:query')")
    public CommonResult<List<EvalProfessionRespVO>> getSimpleProfessionList() {
        List<EvalProfessionDO> list = professionService.getProfessionListByApprovalStatus(1);
        return success(BeanUtils.toBean(list, EvalProfessionRespVO.class));
    }

}
